var http = require('../../utils/http.js')
var util = require('../../utils/util.js')
var localstorage = require('../../utils/localstorage.js')
var appInstance = getApp()
var page = 1;
var pageSize = 15;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectXiLie: {
      id: 0,
      title: "所有系列"
    },
    selectSubXiLie: {
      id: 0,
      title: "所有颜色"
    },

    XiLieArr: [],
    SubXiLieArr: [],
    //
    datalist: [],
    isMore: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getdatalist(
      this.data.selectXiLie,
      this.data.selectSubXiLie,
    );
    this.getXiLielist();
    //this.getSubXiLielist();
  },

   

  



  getXiLielist() {
    var _this = this;
    var postdata = {
      "Page": 1,
      "PageSize": 9999,
      "pid": 0
    }
    http.request({
      url: 'car_colorServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;
        let totalPages = res.result.totalPages

        wx.stopPullDownRefresh()

        _this.setData({
          XiLieArr: [...[_this.data.selectXiLie], ...newArr],
        })


      }
    })
  },

  getSubXiLielist(pid) {
    var _this = this;
    var postdata = {
      "Page": 1,
      "PageSize": 9999,
      "pid": pid
    }
    
    http.request({
      url: 'car_colorServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;
        let totalPages = res.result.totalPages

        wx.stopPullDownRefresh()

        _this.setData({
          SubXiLieArr: [...[_this.data.selectSubXiLie], ...newArr],
        })


      }
    })
  },


  bindPickerChangeXilie: function (e) {

    var index = e.detail.value

    var sdata = this.data.XiLieArr[index]
    if(sdata==undefined)
    {
      return;
    }
    console.log(sdata)
    this.setData({
      selectXiLie: sdata,
      SubXiLieArr: [],
      selectSubXiLie: {
        id: 0,
        title: "所有颜色"
      },
    })

    page = 1;
    this.data.datalist = []
    this.getdatalist(
      sdata,
      this.data.selectSubXiLie,
    );

    this.getSubXiLielist(sdata.id)

  },

  bindPickerChangeXilie2: function (e) {
    var index = e.detail.value

    var sdata = this.data.SubXiLieArr[index]
    if(sdata==undefined)
    {
      return;
    }
    this.setData({
      selectSubXiLie: sdata
    })

    page = 1;
    this.data.datalist = []
    this.getdatalist(
      this.data.selectXiLie,
      sdata,
    );


  },


  
 
  onReachBottom() {
    if (this.data.isMore) {
      this.setData({
        isloading:true
      })
      page = page + 1;
      this.getdatalist(
        this.data.selectXiLie,
        this.data.selectSubXiLie,
      );
    }
  },

  onPullDownRefresh()
  {
    this.setData({
      datalist: [],
      isloading:true
    })
    page = 1;
    this.getdatalist(
      this.data.selectXiLie,
      this.data.selectSubXiLie,
    );

  },

  getdatalist: function (selectXiLie,selectSubXiLie) {
    var _this = this;
    this.setData({
      isloading:true
    })

    var postdata = {
      "Page": page,
      "PageSize": pageSize,
      "xilieId": selectXiLie.id,
      "xilieSubId": selectSubXiLie.id,
      "isRandom":(selectSubXiLie.id+selectXiLie.id)==0
    }
    http.request({
      url: 'car_caseServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {

        let newArr = res.result.items;
        let totalPages = res.result.totalPages

        wx.stopPullDownRefresh()

        _this.setData({
          isloading:false,
          datalist: [..._this.data.datalist, ...newArr],
          isMore: page < totalPages
        })


      }
    })
  },


  onclickItem: function (e) {
    // console.log(e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/pages/case/details?id=' + id + '&title=' + title,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

   

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})